layui.define('table', function(exports) {

	var $ = layui.$,
		table = layui.table,
		transfer = {}
	MOD_NAME = 'transfer',
		Class = function(options) {
			var that = this
			that.config = options
			transfer.options = options
			that.createHTMLDocument()
			that.render()
		}
	transfer.render = function(options) {

		var inst = new Class(options)

	}
	Class.prototype.createHTMLDocument = function() {

		var that = this,
			elem = that.config.elem;
		//			index = that.index;
		//		//创建页面元素
		var html = '';
		html += '<button class="layui-btn submit">保存</button>';
		html += '<div class="layui-row layui-col-space10">';
		html += '<div class="layui-col-md5">';
		html += '<table class="layui-hide" id="left_tab" lay-filter="left"></table>';
		html += '</div>';
		html += '<div class="layui-col-md2 btn-height">';
		html += '<div style="margin-bottom: 10px;">';
		html += '<button class="layui-btn  layui-btn-disabled left-btn">';
		html += '<i class="layui-icon">&#xe602;</i>';
		html += '</button>';
		html += '</div>';
		html += '<div>';
		html += '<button class="layui-btn layui-btn-disabled right-btn">';
		html += '<i class="layui-icon">&#xe603;</i>';
		html += '</button>';
		html += '</div>';
		html += '</div>';
		html += '<div class="layui-col-md5">';
		html += '<table class="layui-hide" id="right_tab" lay-filter="right"></table>';
		html += '</div>';
		html += '</div>';

		$(elem).html(html);
	}
	Class.prototype.render = function() {
		var that = this,
			options = that.config;
		var height = 490; //固定表格高度
		//计算按钮的高度
		var btn_height = height / 2 - 44;
		$('.btn-height').css('padding-top', btn_height).css('text-align', 'center');

		var leftTab = '#left_tab';
		var leftCols = that.config.leftCols;
		var leftUrl = that.config.leftUrl;
		var leftTabId = 'left_table_id';
		var leftTabFilter = 'left';

		var rightCols = leftCols;
		var rightTab = '#right_tab';
		var rightUrl = 'data.php';
		var rightTabId = 'right_table_id';
		var rightTabFilter = 'right';
		//左边表格
		table.render({
			elem: leftTab,
			url: that.config.leftUrl,
			where: {
				type: 1
			},
			cols: that.config.leftCols,
			id: leftTabId,
			page: true,
			height: height
		});
		//右边表格
		table.render({
			elem: rightTab,
			url: rightUrl,
			where: {
				type: 2
			},
			cols: rightCols,
			data: [],
			id: rightTabId,
			page: true,
			height: height
		});

		//监听左表格选中
		table.on('checkbox(' + leftTabFilter + ')', function(obj) {
			btnIf(getTableData(leftTabId), '.left-btn')
		});
		//监听右表格选中
		table.on('checkbox(' + rightTabFilter + ')', function(obj) {
			btnIf(getTableData(rightTabId), '.right-btn')
		});
		//      that.move()

		//重载左边表格
		function reloadTable() {
			var id = layui.data('tabData').id;
			var id_str = id.join(',');
			table.reload(leftTabId, {
				page: {
					curr: 1
				},
				where: {
					id: id_str
				}
			});
			table.reload(rightTabId, {
				page: {
					curr: 1
				},
				where: {
					id: id_str
				}
			});
		}

		//左按钮点击移动数据
		$('.left-btn').on('click', function() {
			if (!$(this).hasClass('layui-btn-disabled')) {
				$(this).addClass('layui-btn-disabled')
				var data = getTableData('left_table_id');
				//查询缓存是否存在数据
				var id = layui.data('tabData').id;
				$.each(data, function(k, v) {
					id.push(v.id)
				});
				//存储缓存
				layui.data('tabData', {
					key: 'id',
					value: id
				})
				reloadTable()
			}
		});
		//右按钮点击移动数据
		$('.right-btn').on('click', function() {
			if (!$(this).hasClass('layui-btn-disabled')) {
				$(this).addClass('layui-btn-disabled')
				var data = getTableData(rightTabId);
				//查询缓存存在的数据
				var id = layui.data('tabData').id;
				$.each(data, function(k, v) {
					id.splice($.inArray(v.id, id), 1) //删除选中的
				});
				//存储缓存
				layui.data('tabData', {
					key: 'id',
					value: id
				})
				reloadTable()
			}
		});

		//默认给值1
		layui.data('tabData', {
			key: 'sumit',
			value: '1'
		})
		$('.submit').on('click', function() {
				//提交后改成O
				layui.data('tabData', {
						key: 'sumit',
						value: '0'
					})
					//关闭窗口
				var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
				parent.layer.close(index); //再执行关闭
			})
			//再次进来赋值
		var id = layui.data('tabData').id;
		if (id && id.length) {
			reloadTable();
		}

	};
	//公共方法
	function getTableData(id) {
		var checkStatus = table.checkStatus(id),
			data = checkStatus.data;
		return data;
	}

	function btnIf(data, btn) {
		if (data && data.length) {
			$(btn).removeClass('layui-btn-disabled')
		} else {
			$(btn).addClass('layui-btn-disabled')
		}
	}
	exports(MOD_NAME, transfer)
})